package com.zzyl.intercept;

import com.zzyl.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description TokenIntercepter
 * @Author loe
 * @Date 2024-07-10
 */

@Component
@Slf4j
public class TokenIntercepter implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("放行前");
        String token = request.getHeader("Authorization");
        if (token == null) {
            log.error("token为null, 返回401");
            response.setStatus(401);
            return false;
        }
        try {
            JwtUtil.parseJWT("itheima", token);
        } catch (Exception e) {
            log.error("token解析错误, 返回401");
            response.setStatus(401);
            return false;
        }
        log.info("token解析成功");
        return true;
    }
}
